package dfbz.com.service.impl;

import dfbz.com.dao.DeptDao;
import dfbz.com.model.Dept;
import dfbz.com.service.DeptService;

import java.util.*;

public class DeptServiceImp implements DeptService {
    DeptDao deptDao = new DeptDao();

    @Override
    public List findDeptMsg() {


        List<Map<String, Object>> deptMsg = deptDao.findDepartments();

        ArrayList<Map<String,Object>> list = new ArrayList<>();
//            set用来存放不重复的部门名字
        HashSet<String> set = new HashSet<>();
        for (Map<String, Object> o : deptMsg) {
            String deptName = o.get("name").toString();
            Object realName = o.get("real_name");
            if (!set.contains(deptName)){
                set.add(deptName);
//                    创建一个部门对象map
                HashMap<String, Object> map = new HashMap<>();
//                    创建一个成员名字list
                ArrayList<String> memberNames = new ArrayList<>();
                map.put("deptName",deptName);
                if (realName!=null){
                    map.put("count",1);
                    memberNames.add(realName.toString());
                    map.put("memberNames",memberNames);
                }else {
                    map.put("count",0);
                }
                list.add(map);
            }else {
//                    也就是说不是第一次加入这个部门，遍历list来拿到对应的map对象，修改count
                for (Map<String, Object> map1 : list) {
                    if (map1.get("deptName").toString().equals(deptName)){
                        String count = map1.get("count").toString();
                        ArrayList<String> memberNames = (ArrayList<String>)map1.get("memberNames");
                        memberNames.add(realName.toString());
                        int i = Integer.parseInt(count);
//                            部门名字重复了，说明该部门肯定至少多于一个人
//                            你这样put进去就是改变了这个map对象了吗？
                        map1.put("count",++i);
                    }
                }
            }
            set.add(o.get("name").toString());
        }
//        System.out.println(list);

        return list;
    }

    @Override
    public List<Dept> findAllDept() {

        List<Dept> list = deptDao.findAllDept();
        return list;
    }
}
